var ns;
var app = new Vue({
    el: '#app',
    data: {
        IsShow:false, //是否显示基本信息
        IsjifenShow:false, //是否显示积分
        memberPrice:'', //金额
        memberIntegral:'',//积分
        form:{},
        roomid:'', //资源位置ID
        fullname:'', //资源位置
        keySearch:'',
        keyRoomSearch:'',
        selType:0,
        convertrule:{} //积分规则
    },
    methods: {
      ToSearch:function(){
       var that = this;
        //搜索功能 调用接口，查询是否有会员信息
        if(that.keySearch!=''){
          ns.post({
              action: 'getmemberinfobyphone',
              search: that.keySearch,
          }, function(succeed, data, err) {
              if (succeed) {
                //alert(JSON.stringify(data));
                if(data!=null){
                  that.form=data;
                  if(data.viewRoomBasics.length>0)
                  {
                    that.roomid= data.viewRoomBasics[0].id;
                    that.fullname= data.viewRoomBasics[0].fullname;
                    that.relationname = data.viewRoomBasics[0].relationname;
                  }
                  that.IsShow=true;
                  that.get_data();
                }
              } else {
                  api.toast({
                      msg: err,
                      duration: 2000,
                      location: 'bottom'
                  });
              }
          });
        }
      },
      ToRoomSearch:function(){
         var that = this;
        //搜索功能 调用接口，查询是否有会员信息
        if(that.keyRoomSearch!=''){
          ns.post({
              action: 'getmemberinfobyphone',
              search: that.keyRoomSearch,
          }, function(succeed, data, err) {
              if (succeed) {
                if(data!=null){
                  that.form=data;
                  if(data.viewRoomBasics.length>0)
                  {
                    that.fullname= data.viewRoomBasics[0].fullname;
                  }
                  that.IsShow=true;
                  that.get_data();
                }
              } else {
                  api.toast({
                      msg: err,
                      duration: 2000,
                      location: 'bottom'
                  });
              }
          });
        }
      },
        //获取积分规则
      get_data: function() {
            var that = this;
            var potions={
              action:'getmallamountpointconvertrulebybusenissid'
            };
            ns.post(potions,function(succeed, data, err) {
                if (succeed) {
                    //alert(JSON.stringify(data));
                    that.convertrule=data;
                    if(that.convertrule!=null)
                    {

                      that.IsjifenShow=true;
                      //alert(that.IsShow);
                    }
                }
            });
        },
      do_save: function() {
            var that = this;
            //alert(that.form.memberPrice);
            if(that.memberPrice==""||that.memberPrice==null||that.memberPrice==0){
              api.toast({
                  msg: '请输入金额',
                  duration: 2000,
                  location: 'bottom'
              });
              return;
            }
           var options={
             action:'addorderandpoint',
             userid:that.form.UserID, //会员ID
             memberPrice:that.memberPrice,//金额
             IsjifenShow:that.IsjifenShow, //是否增加积分
             memberIntegral:that.memberIntegral, //最终积分
             roomid:that.roomid, //资源位置ID
             fullname:that.fullname //资源位置
           };
           ns.post(options, function(succeed, data, err) {
               if (succeed) {

                 api.toast({
                     msg: "提交成功",
                     duration: 3000,
                     location: 'bottom'
                 });
                 api.sendEvent({
                     name: 'saveorder',
                 });

                    api.closeWin();
               }
             });
            // //确认登记操作
            //  if(that.IsjifenShow){
            //     //增加积分
            //  }
            //  else {
            //
            //  }
        },
        do_cancel: function() {
            var that = this;
            //取消登记操作
            api.closeWin();

        },
        onChangePrice:function(){
            var that = this;
            if(that.IsjifenShow)
            {
              //积分兑换规则：1人民币=2积分
              var price=that.memberPrice;
               // 向上取整
              // var ceil = Math.ceil(5 / 2)
              // 向下取整
              // var floor = Math.floor(5 / 2)
              // 四舍五入
              that.memberIntegral= Math.round(price*that.convertrule.ConvertRate);
            }
        },
      getname:function(name){
         if(name.length>1){
           return name.replace(name.substr(0,1),"*");
         }
         else{
            return "";
         }
      }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.selType=api.pageParam.selType;
    // app.mobilePhone = api.pageParam.mobilePhone;
    // app.get_data();
};
